package com.his.controller;

import com.his.common.Rsp;
import com.his.entity.Article;
import com.his.service.impl.FileService;
import com.his.utils.FileUpUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("file")
public class FileController {

    @Resource
    private FileService fileService;

    @RequestMapping("up")
    public Map<String,String> fileUp(MultipartFile file) throws IOException {
        File f = new File("d:/image.png");
        file.transferTo(f);
        String url = FileUpUtil.upload(f);
        f.delete();
        Map<String,String> map=new HashMap<>(1);
        map.put("location","http://qj7iwgvtd.hn-bkt.clouddn.com/"+url);
        return map;
    }

    @PostMapping("doc")
    public Rsp fileUp(@RequestBody Article a) throws IOException {
        return fileService.add(a);
    }

    @GetMapping("getAll")
    public Rsp getAll() {
        return Rsp.success(fileService.getAll());
    }
}
